#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : settings.py
# Purpose  : 
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-09-09 15:38
# ==============================================================================
from PyQt5.QtCore import QSettings

from conf.configuration import VERSION, APPNAME, logger, color_tuple


class Setting(QSettings):
    def __init__(self):
        super(Setting, self).__init__(QSettings.NativeFormat, QSettings.UserScope, 'lyh', 'logviewer')
        logger.debug(f'filename:{self.fileName()}')
        self._init_setting()

    def is_firstly_open(self):
        version_ = self.value('version', '0')  # 获取已存版本号
        if version_ != VERSION:
            # 如果与新版本号不同，则更新版本号，并return True，代表是第一次打开
            self.setValue('version', VERSION)
            logger.info(f"First open, previous version: {version_}, current version {VERSION}")
            return True
        else:
            return False

    def _init_setting(self):
        self.setValue('appName', APPNAME)
        if not self.value('highlighter'):
            self.setValue('highlighter', color_tuple)
